package com.yinhe.music.yhmusic.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.yinhe.music.yhmusic.R;

/**
 * Created by Johnson on 2018/3/31.
 */

public class CircleTextView extends AppCompatTextView {

    private int mBackgroundColor = 0;
    private Paint mPaint;

    public CircleTextView(Context context) {
        super(context);
        init(null);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void setBackgroundColor(int color) {
        mBackgroundColor = color;
        invalidate();
    }

    private void init(AttributeSet attrs) {

        if (attrs != null) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CircleTextView);
            mBackgroundColor = ta.getColor(R.styleable.CircleTextView_circleColor, 0);
            ta.recycle();
        }
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBackgroundColor != 0) {
            mPaint.setColor(mBackgroundColor);
            canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, mPaint);
        }
        super.onDraw(canvas);
    }
}
